﻿using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using BLL;
using Model;

public partial class ProductManage_RFProductManage_Manage_ATEUserManage : System.Web.UI.Page
{
    private ATEUser ateUser;

    public ProductManage_RFProductManage_Manage_ATEUserManage()
    {
        ateUser = new ATEUser();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindATEUser();
        }
    }

    protected void BindATEUser()
    {
        GridView1.DataSource = ateUser.LoadEntities();
        GridView1.DataBind();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        BindATEUser();
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindATEUser();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[5].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
            }
        }
        if ((((DropDownList)e.Row.FindControl("DDLDepartment")) != null) && (((DropDownList)e.Row.FindControl("DDLPostType")) != null))
        {
            DropDownList ddlDepartment = (DropDownList)e.Row.FindControl("DDLDepartment");
            ddlDepartment.DataSource = ATEUser_DepartmentList.list;
            ddlDepartment.DataTextField = "StepText";
            ddlDepartment.DataValueField = "StepValue";
            ddlDepartment.DataBind();
            ddlDepartment.SelectedValue = ((HiddenField)e.Row.FindControl("HDFDepartment")).Value.Trim();

            DropDownList ddlPostType = (DropDownList)e.Row.FindControl("DDLPostType");
            ddlPostType.DataSource = ATEUser_PostTypeList_ForMan.list;
            ddlPostType.DataTextField = "StepText";
            ddlPostType.DataValueField = "StepValue";
            ddlPostType.DataBind();
            ddlPostType.SelectedValue = ((HiddenField)e.Row.FindControl("HDFPostType")).Value.Trim();
        }
        if (((DetailsView)e.Row.FindControl("DetailsView1")) != null)
        {
            DetailsView d = (DetailsView)e.Row.FindControl("DetailsView1");
            if (((DropDownList)d.Rows[2].Cells[1].FindControl("DDLDepartment")) != null && ((DropDownList)d.Rows[3].Cells[1].FindControl("DDLPostType")) != null)
            {
                DropDownList ddlDepartment1 = (DropDownList)d.Rows[2].Cells[1].FindControl("DDLDepartment");
                ddlDepartment1.DataSource = ATEUser_DepartmentList.list;
                ddlDepartment1.DataTextField = "StepText";
                ddlDepartment1.DataValueField = "StepValue";
                ddlDepartment1.DataBind();

                DropDownList ddlPostType1 = (DropDownList)d.Rows[3].Cells[1].FindControl("DDLPostType");
                ddlPostType1.DataSource = ATEUser_PostTypeList_ForMan.list;
                ddlPostType1.DataTextField = "StepText";
                ddlPostType1.DataValueField = "StepValue";
                ddlPostType1.DataBind();
            }
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string UserID = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        ateUser.Delete(UserID);
        BindATEUser();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindATEUser();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        ATEUserInfo ateUserInfo = new ATEUserInfo();
        ateUserInfo.UserID = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        ateUserInfo.UserName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Trim().ToString();
        ateUserInfo.Department = ((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLDepartment"))).SelectedValue;
        ateUserInfo.PostType = ((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLPostType"))).SelectedValue;

        ateUser.Update(ateUserInfo);
        GridView1.EditIndex = -1;
        BindATEUser();
    }

    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        DetailsView d = (DetailsView)this.GridView1.Controls[0].Controls[0].FindControl("DetailsView1");
        ATEUserInfo ateUserInfo = new ATEUserInfo();
        ateUserInfo.UserID = ((TextBox)(d.Rows[0].Cells[1].Controls[0])).Text.Trim();
        ateUserInfo.UserName = ((TextBox)(d.Rows[1].Cells[1].Controls[0])).Text.Trim();
        ateUserInfo.Department = ((DropDownList)(d.Rows[1].Cells[1].FindControl("DDLDepartment"))).SelectedValue;
        ateUserInfo.PostType = ((DropDownList)(d.Rows[3].Cells[1].FindControl("DDLPostType"))).SelectedValue;

        ATEUserInfo ateUserInfo1 = ateUser.LoadEntity(ateUserInfo.UserID);
        if (!string.IsNullOrEmpty(ateUserInfo1.UserID))
        {
            string str1 = "此工号的用户已存在！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else
        {
            ateUser.Insert(ateUserInfo);
            string str1 = "添加成功！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
            BindATEUser();
        }
    }

    protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        BindATEUser();
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Insert")
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
        }
    }
}
